﻿@using Gurux.DLMS.AMI.Components.Enums
@inherits ComponentBase

@if (!_hidden)
{
    <th id="@Id" style="@CssStyle" class="@GetCssClass()" @attributes="@AdditionalAttributes"
        onclick="@(() => SortTable())">
        @ChildContent
        <span class="@(GetSortIcon())"></span>
    </th>
}

@code
{
    /// <summary>
    /// Parent table.
    /// </summary>
    [CascadingParameter]
    private IGXTable? Parent { get; set; }

    /// <summary>
    /// Gets or sets the item template for the list.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Default Sort mode.
    /// </summary>
    [Parameter]
    public SortMode SortMode { get; set; } = SortMode.None;

    /// <summary>
    /// Id.
    /// </summary>
    [Parameter]
    public string? Id { get; set; }

    /// <summary>
    /// CSS style.
    /// </summary>
    [Parameter]
    public string? CssStyle { get; set; }

    /// <summary>
    /// CSS class.
    /// </summary>
    [Parameter]
    public string? CssClass { get; set; } = "sort-th";

    private string? GetCssClass()
    {
        return GetCssClass(Visibility, CssClass);
    }

    internal static string? GetCssClass(Visibility visibility, string? CssClass)
    {
        string? str = null;
        if (visibility == Visibility.ExtraLargeLarge)
        {
            str = "d-none d-xll-table-cell " + CssClass;
        }
        else if (visibility == Visibility.ExtraLarge)
        {
            str = "d-none d-xl-table-cell " + CssClass;
        }
        else if (visibility == Visibility.Large)
        {
            str = "d-none d-lg-table-cell " + CssClass;
        }
        else if (visibility == Visibility.Medium)
        {
            str = "d-none d-md-table-cell " + CssClass;
        }
        else if (visibility == Visibility.Small)
        {
            str = "d-none d-sm-table-cell " + CssClass;
        }
        else if (visibility == Visibility.ExtraSmall)
        {
            str = "d-none d-xs-table-cell " + CssClass;
        }
        else if (visibility == Visibility.Print)
        {
            str = "d-none d-print-table-cell " + CssClass;
        }
        else if (visibility == Visibility.PrintHide)
        {
            str = "d-print-none " + CssClass;
        }
        else
        {
            str = CssClass;
        }

        /*
        if (visibility == Visibility.Large)
        {
            str = "d-none d-xl-table-cell  " + CssClass;
            }
            else if (visibility == Visibility.Medium)
        {
        str = "d-none d-lg-table-cell " + CssClass;
        }
        else if (visibility == Visibility.Small)
            {
            str = "d-none d-md-table-cell " + CssClass;
        }
        else if (visibility == Visibility.ExtraSmall)
            {
            str = "d-none d-sm-table-cell " + CssClass;
            }
        else if (visibility == Visibility.Print)
        {
        str = "d-none d-print-table-cell " + CssClass;
        }
        else if (visibility == Visibility.PrintHide)
        {
        str = "d-print-none " + CssClass;
            }
            else
            {
            str = CssClass;
            }
            */
        return str;
    }

    /// <summary>
    /// Responsive determines the order
    /// in which the contents are hidden if they do not fit the screen.
    /// </summary>
    [Parameter]
    public Visibility Visibility { get; set; } = Visibility.All;

    /// <summary>
    /// Is hidden.
    /// </summary>
    bool _hidden;

    [Parameter(CaptureUnmatchedValues = true)]
    public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

    /// <summary>
    /// Initialize context menu.
    /// </summary>
    protected override void OnInitialized()
    {
        if (Parent == null)
        {
            throw new ArgumentNullException(nameof(Parent), "Th must exist within a GXTable");
        }
        if (SortMode != SortMode.None)
        {
            Parent.SortMode = SortMode;
            Parent.OrderBy = Id;
        }
        _hidden = Parent.IsHidden(Id);
        base.OnInitialized();
    }

    /// <summary>
    /// Sort table with the column.
    /// </summary>
    public void SortTable()
    {
        if (Parent != null)
        {
            if (Id != Parent.OrderBy)
            {
                Parent.OrderBy = Id;
                Parent.SortMode = SortMode.Ascending;
            }
            else
            {
                Parent.SortMode = (SortMode)(((int)++Parent.SortMode) % 3);
            }
            Parent.NotifyShortChange();
        }
    }

    public string GetSortIcon()
    {
        if (Parent != null && Parent.OrderBy == Id)
        {
            switch (Parent.SortMode)
            {
                case SortMode.Ascending:
                    return "fa oi oi-sort-ascending";
                case SortMode.Descending:
                    return "fa oi oi-sort-descending";
            }
        }
        return "";
    }

}